<template>
	<view class="get_eggs">
		<image class="icon_image" src="https://s1.ax1x.com/2022/05/14/OcQjG8.png"></image>
		<view class="back">
			<!-- <label class='eggyue'>余额：{{price.pointsDynamic}}</label> -->
			<view class="flex">
				<label>领取数量：</label>
				<u-number-box :step="10" v-model="eggSum"></u-number-box>
			</view>
			<view class="flex">
				<label>领取方式：</label>
				<view @click="fuck(0)" class="uni-input" :class="index===0?'bac':''">{{typeList[0]}}</view>
				<view @click="fuck(1)" class="uni-input" :class="index===1?'bac':''">{{typeList[1]}}</view>
			</view>
		</view>
		<view @click="handleAddress" :class="index===0?'get_eggs_address':'disdis'">
			<view class="get_eggs_address_left">
				<view class="address_bottom">
					<span>{{address.provinceCityDistrict}}</span>
					<span>{{address.detail}}</span>
				</view>
				<view class="address_top">
					<span>{{address.realName}}</span>
					<span>&nbsp;&nbsp;&nbsp;{{address.phone}}</span>
				</view>
			</view>
			<view class="get_eggs_address_right">
				<span>></span>
			</view>
		</view>

		<view class="get_eggs_footer">
			<view class="">
				自提：10枚的整数起兑
			</view>
			<view class="">
				邮寄：100枚的整数倍起兑
			</view>
		</view>
		<button class="get_eggs_btn" type="primary" @click="handleConfirm">兑换</button>
		<view class="ikun" v-if="flag">
			<view class="ikun1">
				<view>已领取鸡蛋:</view><input v-model="ylqegg" disabled /><span class="jishou">寄售鸡蛋需100枚起</span>
			</view>
			<view class="ikun1">
				<view>待领取鸡蛋:</view><input v-model="dlqegg" disabled />
			</view>
			<view class="ikun2">
				<view>鸡蛋余额:</view><input v-model="price.pointsDynamic" disabled /><span class="jishoujidan"
					@click="jishoujidan">寄售鸡蛋</span>
			</view>
		</view>
		<view class="ikun">
			<view class="ikun1">
				<view>现有匠心鸡:</view><input v-model="chickenCoupons" disabled /><span class="jishou">领取匠心鸡需2只起</span>
			</view>
			<view class="ikun2 ikun3">
				<view>领取数量:</view><input v-model="chickenNum1" /><span class="jishoujidan"
					@click="chickenClass">邮寄匠心</span>
			</view>
			<image class="imgchicken" src="../../static/3x/chicken.png" style="width: 70px;height: 70px;"></image>
		</view>
		<paypass ref="yspay" @cancel="handleClose" @submit="handleSubmit"></paypass>
		<paypass ref="yspay1" @cancel="handleClose" @submit="handleSubmit1"></paypass>
	</view>
</template>

<script>
	import paypass from "@/components/ys-paypass/ys-paypass.vue";
	import util from "@/util/index.js"
	export default {
		components: {
			paypass
		},
		data() {
			return {
				selfMention: {},
				payPassword: "",
				visible: false,
				typeList: ["邮寄", "自提"],
				index: 0,
				eggSum: "10",
				price: {
					pointsDynamic: 0
				},
				address: {
					realName: "",
					provinceCityDistrict: "",
					phone: ""
				},
				ylqegg: 0,
				dlqegg: 0,
				ID: '',
				chickenCoupons: 0,
				chickenNum1: '',
				flag: true,
			}
		},
		onShow() {
			this.getMypage()
			let id = uni.getStorageSync('userSelect')
			if (id) {
				this.$http({
					url: '/crmUserAddress/selUpdAddress',
					method: "GET",
					data: {
						id
					}
				}).then(res => {
					if (res.data.code === 200) {
						this.address = res.data.data
						localStorage.removeItem("userSelect")
					} else {
						uni.showToast({
							title: '请先填写收货地址',
							icon: 'error'
						})
					}
				})
			} else {
				this.$http({
					url: '/crmUserAddress/selCrmUserAddress',
				}).then(res => {
					if (res.data.data.length > 0) {
						this.address = res.data.data[0]
					} else {
						uni.showToast({
							title: '请先填写收货地址',
							icon: 'error'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/locationGo/locationGo'
							})
						}, 500)
					}
				})
			}
			this.getInit()
			setTimeout(() => {
				this.$http({
					url: "/pickUpTheEggs",
					data: {
						id: this.ID
					}
				}).then(res => {
					this.dlqegg = res.data.data.egg
					this.ylqegg = res.data.data.receiveEggs
				})
			}, 1000)
		},
		methods: {
			getMypage() {
				this.$http({
					url: "/member/getMyPage"
				}).then(res => {
					this.ID = res.data.data.memberId
					this.chickenCoupons = res.data.data.chickenCoupons
					this.flag = res.data.data.flag
				})
			},
			//邮寄匠心鸡
			chickenClass() {
				if (this.chickenNum1 === 0 || !this.chickenNum1) {
					uni.showToast({
						title: '请输入要兑换的数量',
						icon: 'error'
					})
					return
				} else if (this.chickenCoupons < this.chickenNum1) {
					uni.showToast({
						title: '兑换数量超过余额，无法兑换',
						icon: 'error'
					})
					return
				} else if (this.chickenNum1 < 2) {
					uni.showToast({
						title: '兑换数量少于2只，无法兑换',
						icon: 'error'
					})
					return
				} else {
					this.visible = true
					this.$refs.yspay1.show();
				}

			},
			jishoujidan() {
				uni.navigateTo({
					url: '/pages/jishoujidan/jishoujidan'
				})
			},
			fuck(index) {
				this.index = index
			},
			getInit() {
				// 查询各类余额
				this.$http({
					url: '/member/getMemberAccount',
				}).then(res => {
					if (res.data.code === 200) {
						this.price = res.data.data
					} else {

					}
				})

				// 查询自提地址
				this.$http({
					url: '/member/getCompanyAddress',
					method: "GET",
				}).then(res => {
					if (res.data.code === 200) {
						this.selfMention = res.data.data[0]
					}
				})
			},
			handleClose() {
				this.payPassword = ""
				this.visible = false
			},
			handleAddress() {
				if (this.index === 0) {
					uni.navigateTo({
						url: '/pages/locationGo/locationGo'
					})
				}
			},
			handleConfirm() {
				if (this.eggSum === 0 || !this.eggSum) {
					uni.showToast({
						title: '请输入要兑换的数量',
						icon: 'error'
					})
					return
				} else if (this.eggSum > this.price.pointsDynamic) {
					uni.showToast({
						title: '兑换数量超过余额，无法兑换',
						icon: 'error'
					})
					return
				}
				if (this.index === 1) {
					if (this.eggSum % 10 === 0) {
						this.visible = true
						this.$refs.yspay.show();
					} else {
						uni.showToast({
							title: '数量不正确',
							icon: 'error'
						})
					}
				} else {
					if (this.eggSum % 100 === 0) {
						this.visible = true
						this.$refs.yspay.show();
					} else {
						uni.showToast({
							title: '数量不正确',
							icon: 'error'
						})
					}
				}
			},
			//兑换鸡蛋
			handleSubmit(ex) {
				let index = JSON.parse(JSON.stringify(this.index))
				this.$http({
					url: '/crmOrder/insCrmOrder',
					method: "POST",
					data: {
						id: "1",
						orderType: "2",
						deliveryStatus: index,
						harvestAddressId: this.address.id,
						num: this.eggSum,
						deliverMoney: 0,
						totalAmount: 0,
						amount: this.eggSum,
						processingFee: 0,
						commodityPoints: 0,
						orderSrc: "3",
						payType: "8",
						phone: this.address.phone,
					}
				}).then(res => {
					if (res.data.code === 200) {
						this.handleExchange(res.data.data.orderNo, ex.value)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			//邮寄匠心鸡
			handleSubmit1(ex) {
				let index = JSON.parse(JSON.stringify(this.index))
				this.$http({
					url: '/crmOrder/insCrmOrder',
					method: "POST",
					data: {
						id: "1",
						orderType: "2",
						deliveryStatus: index,
						harvestAddressId: this.address.id,
						num: this.chickenNum1,
						deliverMoney: 0,
						totalAmount: this.chickenNum1,
						amount: this.chickenNum1,
						processingFee: 0,
						commodityPoints: 0,
						orderSrc: "3",
						payType: "9",
						phone: this.address.phone,
					}
				}).then(res => {
					if (res.data.code === 200) {
						this.handleExchange1(res.data.data.orderNo, ex.value)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			handleExchange(orderNo, password) {
				this.$http({
					url: '/eggExchange/conversionGoodsPay',
					method: "POST",
					data: {
						payType: "2",
						password: password,
						orderNo: orderNo,
						payPrice: this.eggSum,
						craftPrice: this.eggSum
					}
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '兑换成功',
						})
						this.payPassword = ""
						this.eggSum = ""
						this.getInit()
						this.visible = false
						// setTimeout(() => {
						// 	uni.switchTab({
						// 		url: "/pages/index/index"
						// 	})
						// }, 1000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			handleExchange1(orderNo, password) {
				this.$http({
					url: '/payChicken',
					method: "POST",
					data: {
						payType: "2",
						password: password,
						orderNo: orderNo,
						payPrice: this.chickenNum1,
						craftPrice: this.chickenNum1,
						memberId: this.ID, //用户id
					}
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '邮寄成功',
						})
						this.payPassword = ""
						this.chickenNum1 = ""
						this.getInit()
						this.visible = false
						// setTimeout(() => {
						// 	uni.switchTab({
						// 		url: "/pages/index/index"
						// 	})
						// }, 1000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
					this.getMypage()
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import './getEggs.css'
</style>
